package br.ufpe.cin.vspeed.gui;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;

import org.apache.commons.lang.StringUtils;

import speed2010.reformulacao.enums.EnrichmentType;

public class PanelEnrichingVariables extends javax.swing.JPanel {
	private static final long serialVersionUID = 577508952955958695L;

	private JPanel jPanelVariables;
	private JList jListPriorities;
	private JLabel jLabel3;
	private JButton jButtonUp;
	private JButton jButtonDown;
	private JPanel jPanelPriority;
	private JList jListVariables;
	private JButton jButtonRight;
	private JButton jButtonLeft;
	private JLabel jLabel2;

	private final DefaultListModel variablesBegin;
	private final DefaultListModel variablesPriority;

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */
	public static void main(final String[] args) {
		final JFrame frame = new JFrame();
		frame.getContentPane().add(new PanelEnrichingVariables());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public PanelEnrichingVariables() {
		super();
		this.variablesBegin = new DefaultListModel();
		this.variablesPriority = new DefaultListModel();
		initGUI();
	}

	private void initGUI() {
		try {
			setFont(new java.awt.Font("Segoe UI", 0, 11));
			setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED),
					" Enriching variables:", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION, new java.awt.Font(
							"Segoe UI", 1, 12), new java.awt.Color(0, 0, 0)));
			final GroupLayout thisLayout = new GroupLayout(this);
			setLayout(thisLayout);
			{
				this.jPanelVariables = getJPanelVariables();
				this.jPanelPriority = getJPanelPriority();
			}
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
					.addComponent(this.jPanelVariables, GroupLayout.PREFERRED_SIZE, 175, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(this.jPanelPriority, GroupLayout.PREFERRED_SIZE, 229, GroupLayout.PREFERRED_SIZE)
					.addContainerGap());
			thisLayout.setVerticalGroup(thisLayout
					.createSequentialGroup()
					.addGap(7)
					.addGroup(
							thisLayout
									.createParallelGroup()
									.addComponent(this.jPanelVariables, GroupLayout.Alignment.LEADING, 0, 107,
											Short.MAX_VALUE)
									.addComponent(this.jPanelPriority, GroupLayout.Alignment.LEADING, 0, 107,
											Short.MAX_VALUE)).addContainerGap());
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	private void selectPriorites() {
		if (this.jListPriorities.getSelectedValue() != null) {
			final String variableSelected = this.jListPriorities.getSelectedValue().toString();
			this.variablesPriority.removeElement(variableSelected);
			this.variablesBegin.addElement(variableSelected);
		}

	}

	private void selectVariables() {
		if (this.jListVariables.getSelectedValue() != null) {
			final String variableSelected = this.jListVariables.getSelectedValue().toString();
			this.variablesBegin.removeElement(variableSelected);
			this.variablesPriority.addElement(variableSelected);
		}

	}

	public DefaultListModel getVariablesPriority() {
		return this.variablesPriority;
	}

	private JPanel getJPanelPriority() {
		if (this.jPanelPriority == null) {
			this.jPanelPriority = new JPanel();
			final GroupLayout jPanelPriorityLayout = new GroupLayout(this.jPanelPriority);
			this.jPanelPriority.setLayout(jPanelPriorityLayout);
			{
				this.jListPriorities = new JList(this.variablesPriority);
				this.jListPriorities.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				this.jListPriorities.addMouseListener(new java.awt.event.MouseAdapter() {
					@Override
					public void mouseClicked(final java.awt.event.MouseEvent e) {
						if (e.getClickCount() == 2) {
							selectPriorites();
						}
					}
				});
			}
			{
				this.jLabel3 = new JLabel();
				this.jLabel3.setText("Priority");
				this.jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
				this.jLabel3.setFont(new java.awt.Font("Segoe UI", 0, 11));
			}
			jPanelPriorityLayout.setHorizontalGroup(jPanelPriorityLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							jPanelPriorityLayout
									.createParallelGroup()
									.addGroup(
											GroupLayout.Alignment.LEADING,
											jPanelPriorityLayout
													.createSequentialGroup()
													.addComponent(this.jLabel3, GroupLayout.PREFERRED_SIZE, 120,
															GroupLayout.PREFERRED_SIZE).addGap(0, 7, Short.MAX_VALUE))
									.addComponent(this.jListPriorities, GroupLayout.Alignment.LEADING, 0, 127,
											Short.MAX_VALUE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(
							jPanelPriorityLayout
									.createParallelGroup()
									.addComponent(getJButtonDown(), GroupLayout.Alignment.LEADING,
											GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
									.addComponent(getJButtonUp(), GroupLayout.Alignment.LEADING,
											GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE))
					.addContainerGap());
			jPanelPriorityLayout.setVerticalGroup(jPanelPriorityLayout
					.createSequentialGroup()
					.addComponent(this.jLabel3, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE,
							GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(
							jPanelPriorityLayout
									.createParallelGroup()
									.addGroup(
											GroupLayout.Alignment.LEADING,
											jPanelPriorityLayout
													.createSequentialGroup()
													.addComponent(getJButtonUp(), GroupLayout.PREFERRED_SIZE, 29,
															GroupLayout.PREFERRED_SIZE)
													.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
													.addComponent(getJButtonDown(), GroupLayout.PREFERRED_SIZE, 29,
															GroupLayout.PREFERRED_SIZE).addGap(0, 6, Short.MAX_VALUE))
									.addComponent(this.jListPriorities, GroupLayout.Alignment.LEADING, 0, 70,
											Short.MAX_VALUE)).addContainerGap());
		}
		return this.jPanelPriority;
	}

	private JPanel getJPanelVariables() {
		if (this.jPanelVariables == null) {
			this.jPanelVariables = new JPanel();
			final GroupLayout jPanelVariablesLayout = new GroupLayout(this.jPanelVariables);
			this.jPanelVariables.setLayout(jPanelVariablesLayout);
			this.jPanelVariables.setBounds(290, 320, 104, 51);
			{
				this.variablesBegin.addElement(StringUtils.capitalize(EnrichmentType.APPROXIMATE.getType()));
				this.variablesBegin.addElement(StringUtils.capitalize(EnrichmentType.COMPOSE.getType()));
				this.variablesBegin.addElement(StringUtils.capitalize(EnrichmentType.GENERALIZE.getType()));
				this.variablesBegin.addElement(StringUtils.capitalize(EnrichmentType.SPECIALIZE.getType()));

				this.jListVariables = new JList(this.variablesBegin);
				this.jListVariables.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				this.jListVariables.addMouseListener(new java.awt.event.MouseAdapter() {
					@Override
					public void mouseClicked(final java.awt.event.MouseEvent e) {
						if (e.getClickCount() == 2) {
							selectVariables();
						}
					}
				});
			}
			{
				this.jButtonRight = new JButton();
				this.jButtonRight.setLayout(null);
				this.jButtonRight.setText(">>");
				this.jButtonRight.setBorder(BorderFactory.createCompoundBorder(null, null));
				this.jButtonRight.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(final java.awt.event.ActionEvent e) {
						selectVariables();
					}
				});

			}
			{
				this.jButtonLeft = new JButton();
				this.jButtonLeft.setText("<<");
				this.jButtonLeft.setBorder(BorderFactory.createCompoundBorder(null, null));
				this.jButtonLeft.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(final java.awt.event.ActionEvent e) {
						selectPriorites();
					}
				});

			}
			{
				this.jLabel2 = new JLabel();
				this.jLabel2.setText("Variables");
				this.jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
				this.jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 11));
			}
			jPanelVariablesLayout
					.setHorizontalGroup(jPanelVariablesLayout
							.createParallelGroup()
							.addGroup(
									GroupLayout.Alignment.LEADING,
									jPanelVariablesLayout
											.createSequentialGroup()
											.addComponent(this.jListVariables, 0, 130, Short.MAX_VALUE)
											.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 6,
													GroupLayout.PREFERRED_SIZE)
											.addGroup(
													jPanelVariablesLayout
															.createParallelGroup()
															.addComponent(this.jButtonLeft,
																	GroupLayout.Alignment.LEADING,
																	GroupLayout.PREFERRED_SIZE, 33,
																	GroupLayout.PREFERRED_SIZE)
															.addComponent(this.jButtonRight,
																	GroupLayout.Alignment.LEADING,
																	GroupLayout.PREFERRED_SIZE, 33,
																	GroupLayout.PREFERRED_SIZE)))
							.addGroup(
									GroupLayout.Alignment.LEADING,
									jPanelVariablesLayout
											.createSequentialGroup()
											.addPreferredGap(this.jListVariables, this.jLabel2,
													LayoutStyle.ComponentPlacement.INDENT)
											.addComponent(this.jLabel2, GroupLayout.PREFERRED_SIZE, 127,
													GroupLayout.PREFERRED_SIZE).addContainerGap(32, Short.MAX_VALUE)));
			jPanelVariablesLayout.setVerticalGroup(jPanelVariablesLayout
					.createSequentialGroup()
					.addComponent(this.jLabel2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE,
							GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(
							jPanelVariablesLayout
									.createParallelGroup()
									.addGroup(
											GroupLayout.Alignment.LEADING,
											jPanelVariablesLayout
													.createSequentialGroup()
													.addComponent(this.jButtonRight, GroupLayout.PREFERRED_SIZE, 29,
															GroupLayout.PREFERRED_SIZE)
													.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
													.addComponent(this.jButtonLeft, GroupLayout.PREFERRED_SIZE, 29,
															GroupLayout.PREFERRED_SIZE).addGap(0, 10, Short.MAX_VALUE))
									.addComponent(this.jListVariables, GroupLayout.Alignment.LEADING, 0, 79,
											Short.MAX_VALUE)).addContainerGap());
		}
		return this.jPanelVariables;
	}

	private JButton getJButtonUp() {
		if (this.jButtonUp == null) {
			this.jButtonUp = new JButton();
			this.jButtonUp.setText("Up");
			this.jButtonUp.setFont(new java.awt.Font("Dialog", 0, 11));

			this.jButtonUp.addActionListener(new java.awt.event.ActionListener() {
				@Override
				public void actionPerformed(final java.awt.event.ActionEvent e) {
					final int selectedIndex = PanelEnrichingVariables.this.jListPriorities.getSelectedIndex();
					if (selectedIndex > 0) {
						final String temp = PanelEnrichingVariables.this.variablesPriority.get(selectedIndex)
								.toString();
						final Object selectedElement = PanelEnrichingVariables.this.variablesPriority
								.get(selectedIndex - 1);
						PanelEnrichingVariables.this.variablesPriority.setElementAt(selectedElement.toString(),
								selectedIndex);
						PanelEnrichingVariables.this.variablesPriority.setElementAt(temp, selectedIndex - 1);
						PanelEnrichingVariables.this.jListPriorities.setSelectedIndex(selectedIndex - 1);
					}
				}
			});
		}

		return this.jButtonUp;
	}

	private JButton getJButtonDown() {
		if (this.jButtonDown == null) {
			this.jButtonDown = new JButton();
			this.jButtonDown.setText("Down");
			this.jButtonDown.setFont(new java.awt.Font("Dialog", 0, 11));
			this.jButtonDown.addActionListener(new java.awt.event.ActionListener() {
				@Override
				public void actionPerformed(final java.awt.event.ActionEvent e) {
					final int selectedIndex = PanelEnrichingVariables.this.jListPriorities.getSelectedIndex();
					if (selectedIndex < (PanelEnrichingVariables.this.variablesPriority.size() - 1)) {
						final String temp = PanelEnrichingVariables.this.variablesPriority.get(selectedIndex)
								.toString();
						final Object selectElement = PanelEnrichingVariables.this.variablesPriority
								.get(selectedIndex + 1);
						PanelEnrichingVariables.this.variablesPriority.setElementAt(selectElement.toString(),
								selectedIndex);
						PanelEnrichingVariables.this.variablesPriority.setElementAt(temp, selectedIndex + 1);
						PanelEnrichingVariables.this.jListPriorities.setSelectedIndex(selectedIndex + 1);
					}
				}
			});

		}

		return this.jButtonDown;
	}

}
